﻿var equipmentListTableVue = new Vue({
    el: "#equipmentListTable",
    data: {
        isImportDisabled: false,    //用于上传时禁用按钮
        isExportDisabled: false,    //用于导出时禁用按钮
        isImportLoading: false,     //用于上传时等待
        isExportLoading: false,     //用于导出时等待
        uploadFile: null,           //接收上传文件
        uploadFileName: "",         //显示上传文件名
        pageModel: {                
            pageIdx: 1,             
            pageSize: 10,           
            pageTotal: 0,           
            pageLength: 0,          
            reults: []              
        },
        searchModel: {              
            queryCode: "",          
            queryName: "",          
            queryModel: "",
            querySn: "",
            queryClient: "",
            queryStatus: "",        
            queryAssignerID: "",
            queryManagerID: "",
            orderByCode: false,
            orderByName: false,     
            isAsc: true,            
            isAll: true             
        },
        typeDictionary: {},         //设备类型字典
        statusDictionary: {},        //设备状态字典
        assignerDictionary: {},     //负责人字典
        managerDictionary: {}       //管理人字典
    },
    methods: {
        SelectFile: function (event) {
            var vue = this;
            event.preventDefault();

            //由于IE兼容问题（input只响应一次onchange），采用每次生成input[type=file]方法
            $(event.target).next("input[type=file]").remove();
            var uploadTab = '<input type="file" style="display: none" accept=".xls, .xlsx" onchange="SetFileName(this)" />';
            $(event.target).after(uploadTab);
            $(event.target).next().click();
        },
        UploadFile: function () {
            if (this.uploadFile != null) {
                UploadFileToServer(this);
            }
        },
        GotoPage: function (toPageIdx) {
            this.pageModel.pageIdx = toPageIdx;
            ListEquipmentForPage(this);
        },
        SearchEquipment: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.isAll = false;
            ListEquipmentForPage(this);
        },
        SearchReset: function () {
            ResetPageModel(this);
            ListEquipmentForPage(this);
        },
        ChangeOrderByName: function (event) {
            $("a i.fa-angle-up").not(event.target).removeClass("fa-angle-down");
            $(event.target).toggleClass('fa-angle-down');
            this.searchModel.isAsc = $(event.target).hasClass('fa-angle-down') === false;
            this.searchModel.orderByName = true;
            this.searchModel.orderByCode = false;
            ListEquipmentForPage(this);
        },
        ChangeOrderByCode: function (event) {
            $("a i.fa-angle-up").not(event.target).removeClass("fa-angle-down");
            $(event.target).toggleClass('fa-angle-down');
            this.searchModel.isAsc = $(event.target).hasClass('fa-angle-down') === false;
            this.searchModel.orderByName = false;
            this.searchModel.orderByCode = true;
            ListEquipmentForPage(this);
        },
        AddEquipment: function () {
            AddEquipmentDetail(this);
        },
        EditEquipment: function (equipmentID) {
            EditEquipmentDetail(this, equipmentID);
        },
        DeleteEquipment: function (equipmentID) {
            DeleteEquipmentDetail(this, equipmentID);
        },
        ExportEquipment: function () {
            ExportEquipmentDetail(this);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.typeDictionary = GlobalDictionary.GetEquipmentTypeDictionary();
        this.statusDictionary = GlobalDictionary.GetEquipmentStatusDictionary();
        this.assignerDictionary = GlobalDictionary.GetEquipmentAssignerDictionary();
        this.managerDictionary = GlobalDictionary.GetEquipmentManagerDictionary();

        console.log(this.assignerDictionary);
        console.log(this.managerDictionary);

        ResetToastrOption();
        ResetPageModel(this);
        ListEquipmentForPage(this);
    },
    watch: {
        "pageModel.pageSize": function (val) {
            this.pageModel.pageIdx = 1;
            ListEquipmentForPage(this);
        }
    }
});

/**
 * 显示上传文件名
 */
function SetFileName(event) {
    var vue = equipmentListTableVue;
    vue.uploadFileName = $(event).context.files[0].name;
    vue.uploadFile = $(event).context.files[0];
}

/**
 * 列出所有设备信息
 */
function ListEquipmentForPage(vue) {
    vue.pageModel.results = [];
    ShowAzLoading();

    $.ajax({
        type: "POST",
        url: "/Equipment/ListEquipmentForPage?v=" + Math.random(),
        data: {
            pageModel: vue.pageModel,
            searchModel: vue.searchModel
        },
        success: function (res) {
            console.log(res);
            if (res.status == "Success") {
                vue.pageModel = res.data;
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 * 新增设备信息
 */
function AddEquipmentDetail(vue) {
    OpenAzModal("新增设备信息New equipment information", "/Equipment/Page/Detail?IsNew=true", function () {
        ListEquipmentForPage(vue);
    });
    ResizeAzModal(1000);
}

/**
 * 编辑设备信息
 */
function EditEquipmentDetail(vue, equipmentID) {
    OpenAzModal("编辑设备信息Edit device information", "/Equipment/Page/Detail?EquipmentID=" + equipmentID, function () {
        ListEquipmentForPage(vue);
    });
    ResizeAzModal(1000);
}

function DeleteEquipmentDetail(vue, equipmentID) {
    if (confirm("确认删除该设备Confirm to delete the device?")) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Equipment/DeleteEquipment?v=" + Math.random(),
            data: { "equipmentID": equipmentID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除设备成功Device deleted successfully。");
                    ListEquipmentForPage(vue);
                } else {
                    toastr.error(res.message);
                }
            },
            error: function (err) {
                console.log(err);
                toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
            },
            complete: function () {
                HideAzLoading();
            }
        });
    }   
}

/**
 * 上传导入文件到服务器，在服务器处理完成返回导入结果数
 */
function UploadFileToServer(vue) {
    var formData = new FormData();
    formData.append("file", vue.uploadFile);

    vue.isImportLoading = true;
    vue.isImportDisabled = true;

    $.ajax({
        type: "POST",
        url: "/Equipment/ImportEquipmentData",
        data: formData,
        contentType: false,         //设置false会自动添加正确Content-Type
        processData: false,         //设置false避开JQuery对FormData的默认处理
        success: function (res) {
            vue.uploadFile = null;
            vue.uploadFileName = "";

            if (res.status == "Success") {
                toastr.success("成功导入Successfully imported【" + res.data + "】数据data");
                ListEquipmentForPage(vue);
            } else {
                toastr.error(res.message);
            }
        },
        error: function (err) {
            console.log(err);
            toastr.error("加载数据异常，请稍后再试或联系管理员Abnormal data loading. Please try again later or contact the administrator。");
        },
        complete: function () {
            vue.isImportLoading = false;
            vue.isImportDisabled = false;
        }
    });
};

/**
 * 导出设备信息
 */
function ExportEquipmentDetail(vue) {
    vue.isExportLoading = true;
    vue.isExportDisabled = true;

    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/Equipment/ExportEquipmentData?v=" + Math.random(),
        data: {
            searchModel: vue.searchModel
        },
        success: function (res) {
            if (res.status == "Success") {
                window.location.href = res.data;
            } else {
                toastr.error(res.message);
            }
        },
        error: function (err) {
            console.log(err);
            toastr.error("加载数据异常，请稍后再试或联系管理员Abnormal data loading. Please try again later or contact the administrator。");
        },
        complete: function () {
            HideAzLoading();
            vue.isExportLoading = false;
            vue.isExportDisabled = false;
        }
    });
}

function ResetPageModel(vue) {
    vue.pageModel.pageIdx = 1;
    vue.pageModel.pageSize = 10;
    vue.pageModel.pageLength = 0;
    vue.pageModel.pageTotal = 0;

    vue.searchModel.queryName = "";
    vue.searchModel.queryCode = "";
    vue.searchModel.querySn = "";
    vue.searchModel.queryModel = "";
    vue.searchModel.queryStatus = "";
    vue.searchModel.queryClient = "";
    vue.searchModel.queryAssignerID = "";
    vue.searchModel.queryManagerID = "";
    vue.searchModel.orderByName = false;
    vue.searchModel.orderByCode = false;
    vue.searchModel.isAsc = true;
    vue.searchModel.isAll = true;
}

